package com.boot.main.service.impl;

import com.boot.main.entity.CcomUser;
import com.boot.main.entity.ScomCategory;
import com.boot.main.mapper.CcomUserMapper;
import com.boot.main.mapper.ScomCategoryMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.ICategoryService;
import com.boot.main.utils.IsNotEmptyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryService implements ICategoryService {
    @Resource
    private ScomCategoryMapper categoryMapper;

    @Resource
    private CcomUserMapper userMapper;

    public Long getUserId(){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals =  subject.getPrincipals();
        String username = (String) principals.getPrimaryPrincipal();
        CcomUser user = userMapper.getUserByUserName(username);
        Long id = user.getId();
        return id;
    }

    @Override
    public ResponseData getCategorys(){
        Long userId = getUserId();
        List<ScomCategory> items = categoryMapper.getCategorys(userId);
        return ResponseDataUtil.success("获取分类成功", items);
    }

    @Override
    public ResponseData getAdminCategorys(){
        CcomUser user = userMapper.getUserByUserName("superAdmin");
        List<ScomCategory> items = categoryMapper.getCategorys(user.getId());
        return ResponseDataUtil.success("获取分类成功", items);
    }

    @Override
    public ResponseData getCategoryByStatus(Integer status){
        Long userId = getUserId();
        List<ScomCategory> items = categoryMapper.getCategoryByStatus(status, userId);
        return ResponseDataUtil.success("获取分类成功", items);
    }

    @Override
    public ResponseData getAdminCategoryByStatus(Integer status){
        CcomUser user = userMapper.getUserByUserName("superAdmin");
        List<ScomCategory> items = categoryMapper.getCategoryByStatus(status, user.getId());
        return ResponseDataUtil.success("获取分类成功", items);
    }

    @Override
    public ResponseData saveCategory(ScomCategory category){
        category.setCreate_time(new Date().getTime());
        Integer num = categoryMapper.saveCategory(category);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("保存分类失败", null);
        }
        return ResponseDataUtil.success("保存分类成功", null);
    }

    @Override
    public ResponseData updateCategory(ScomCategory category){
        category.setCreate_time(new Date().getTime());
        Integer num = categoryMapper.updateCategory(category);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改分类失败", null);
        }
        return ResponseDataUtil.success("修改分类成功", null);
    }

    @Override
    public ResponseData deleteCategory(Long id){
        Integer num = categoryMapper.deleteCategory(id);
        return ResponseDataUtil.success("删除分类成功", null);
    }
}
